import Config from '../config.js'; 

class Base_model {
  
  constructor() {
  }

  request(params) {
    var that = this;
    
    if (! params.method) {
      params.method = 'GET';
    }    

    wx.request({
      url: Config.apiBaseUrl + params.url,
      data: params.data,
      header: {
        'Content-Type':'application/json',
        'token':wx.getStorageInfoSync('token')
      },
      method: params.method,
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        var statusCode = res.statusCode.toString();
        var startChar = statusCode.charAt(0);
        
        console.log(res.data);
        
        if (startChar == '2') {
            params.successCallback && params.successCallback(res.data);
        } else {
          that.processError(res);
          params.errorCallback && params.errorCallback(res.data);
        }

      },
      fail: function (res) {
        that.processError(res);
      }
    }) 
  }

  processError(error) {
    if (error.hasOwnProperty("data") && error.data.message) {
      wx.showToast({
        title: error.data.message,
        // icon:"error",   //todo:要换个错误图标
        duration: 2000
      });
    }
    console.log(error);
  }

  /*获得元素上的绑定的值*/
  getDataSet(event, key) {
    return event.currentTarget.dataset[key];
  }
}

export {Base_model}